package com.dzpk.gameserver.module.main;
/*
 * $RCSfile: RobotPock.java,v $
 * $Revision: 1.1  $
 * $Date: 2012-11-20  $
 *
 * Copyright (C) 2005 Bettem, Inc. All rights reserved.
 *
 * This software is the proprietary information of Bettem, Inc.
 * Use is subject to license terms.
 */

import com.dzpk.gameserver.module.main.pocer.Pocer;

import java.util.Random;


/**
 * <p>
 * Title: RobotPock
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2006
 * </p>
 *
 * @author shenxing.ruan
 * @version 1.0
 */
public class RobotPock {
    private static Random r = new Random();

    /**
     * 机器人获取指定类型牌
     *
     * @param type
     * @return
     */
    public static int[] GetCards(int type) {
        int[] p1 = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; // 黑桃
        int[] p2 = new int[]{13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
                25}; // 红桃
        int[] p3 = new int[]{26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
                38}; // 梅花
        int[] p4 = new int[]{39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
                51}; // 方块

        int[][] cards2 = new int[][]{p1, p2, p3, p4}; // 一副扑克牌
        int[] cards = new int[5]; // 五张牌
        int[] hs = random(4); // 四种花色
        int next = r.nextInt(4); // 随机选中一种花色
        int index = r.nextInt(13); // 相同花色牌抽牌位置
        int n = 0;
        if (type == 0) {
            type = 2;
        }
        // 同花顺
        if (type == 0) {
            int[] pocer = cards2[next];
            int start = r.nextInt(6) + 3;
            for (int i = 0; i < 5; i++) {
                cards[i] = pocer[start];
                start++;
            }
        }
        // 四条
        else if (type == 1) {
            int lastIndex = index > next ? index - next - 1 : next + index + 2; // 最后一个数索引
            for (int i : hs) {
                cards[n] = cards2[i][index];
                n++;
            }
            cards[4] = cards2[next][lastIndex];
        }
        // 葫芦
        else if (type == 2) {
            int lastIndex = index > next ? index - next - 1 : next + index + 2; // 后面对子索引
            for (int i : hs) {
                cards[n] = cards2[i][index];
                n++;
            }
            cards[3] = cards2[next > 0 ? next - 1 : next + 1][lastIndex];
            cards[4] = cards2[next][lastIndex];
        }
        // 同花
        else if (type == 3) {
            int[] pocer = cards2[next];
            int[] indexs = random(13);
            for (int i = 0; i < 5; i++) {
                cards[i] = pocer[indexs[i]];
            }
        }
        // 顺子
        else if (type == 4) {
            index = r.nextInt(6) + 3;
            for (int i : hs) {
                cards[n] = cards2[i][index + n];
                n++;
            }
            cards[4] = cards2[3][index + 4];
        }
        // 三条
        else if (type == 5) {
            for (int i : hs) {
                cards[n] = cards2[i][index];
                n++;
            }
            int index3 = r.nextInt(7);
            cards[3] = cards2[next][index3 == index ? index3 + 1 : index3];
            int index4 = r.nextInt(6) + 7;
            cards[4] = cards2[next][index4 == index ? index4 - 1 : index4];
        }
        // 两对
        else if (type == 6) {
            // 第一对
            int t1 = r.nextInt(3) + 7;
            cards[0] = cards2[hs[0]][t1];
            cards[1] = cards2[hs[1]][t1];
            // 第二对
            int t2 = r.nextInt(3) + 10;
            cards[2] = cards2[hs[2]][t2];
            cards[3] = cards2[hs[3]][t2];
            // 第五张牌
            cards[4] = cards2[hs[0]][t1 - 1];
        }
        return cards;
    }

    /**
     * 产生随机数
     *
     * @return
     */
    private static int[] random(int size) {
        int[] send = new int[size];
        for (int i = 0; i < size; i++) {
            send[i] = i;
        }
        if (size > 1) {
            int temp1, temp2, temp3;
            for (int i = 0; i < send.length; i++) { // 随机交换send.length次
                temp1 = Math.abs(r.nextInt()) % (send.length - 1); // 随机产生一个位置
                temp2 = Math.abs(r.nextInt()) % (send.length - 1); // 随机产生另一个位置
                if (temp1 != temp2) {
                    temp3 = send[temp1];
                    send[temp1] = send[temp2];
                    send[temp2] = temp3;
                }
            }
        }
        return send;
    }

    /**
     * 测试方法
     *
     * @param args
     */
    public static void main(String[] args) {
        int[] ints = {1, 2, 3, 4, 5, 6};

        for (int j = 0; j < ints.length; j++) {
            for (int i = 0; i < 50; i++) {
                int pocks[] = GetCards(ints[j]);
                StringBuilder sb = new StringBuilder();
                for (int k = 0; k < pocks.length; k++) {
                    sb.append(new Pocer(pocks[k]).getName()).append("/");
                }
                System.out.println(sb.toString());
            }
            System.out.println("-----------------------------------------------");
            System.out.println("-----------------------------------------------");
        }
        // // for(int i = 0; i < 7; i++) {
        // // getZuidapai();
        // // }
        // for (int i = 0 ; i < 200 ; i ++) {
        // int[] ins = GetCards(1);
        // System.out.println(new Pocer(ins[0]).getSize2() + " " + new
        // Pocer(ins[1]).getSize2() + " " + new Pocer(ins[2]).getSize2() + " " +
        // new Pocer(ins[3]).getSize2() + " " + new Pocer(ins[4]).getSize2());
        // }
        // System.out.println(r.nextInt(2));
        // System.out.println(r.nextInt(2));
        //
        // System.out.println(r.nextInt(2));
        // System.out.println(r.nextInt(2));
        // System.out.println(r.nextInt(2));
        // System.out.println(r.nextInt(2));
        // System.out.println(r.nextInt(2));
        // Map<Integer, String> map = new HashMap<Integer, String>();
        // map.put(0, "同花顺");
        // map.put(1, "四条");
        // map.put(2, "葫芦");
        // map.put(3, "同花");
        // map.put(4, "顺子");
        // map.put(5, "三条");
        // map.put(6, "两对");
        // for (int i = 0 ; i < 200 ; i ++) {
        // int[] ins = getZuidapai();
        // for (int i2 : ins) {
        // Pocer p = new Pocer(i2);
        // System.out.println((p.getType() == 1 ? "方块" : p.getType() == 2? "草花"
        // : p.getType() == 3 ? "红桃" : "黑桃") + p.getSize2() + " / " +
        // p.getSize1() );
        // }
        // System.out.println("--------------------");
        // }

    }

    /**
     * 获取大牌
     *
     * @return
     */
    public static int[] getZuidapai() {
        // 0 - 6 00 22 44 66 88 100 105 110 115
        int i = r.nextInt(110);
        int type = 0;
        if (i <= 10) {
            type = 6;
        } else if (10 < i && i <= 34) {
            type = 5;
        } else if (34 < i && i <= 56) {
            type = 4;
        } else if (56 < i && i <= 78) {
            type = 3;
        } else if (78 < i && i <= 100) {
            type = 2;
        } else if (100 < i && i <= 105) {
            type = 1;
        } else if (105 < i && i <= 110) {
            type = 0;
        }
        // System.out.println(type);
        int[] ints = GetCards(type);
        for (int j = 0; j < ints.length; j++) {
            int b = r.nextInt(4);
            int c = ints[b];
            ints[b] = ints[j];
            ints[j] = c;
        }
        return ints;
    }

    /**
     * 获取大牌
     *
     * @return
     */
    public static int[] getZuidapai2() {
        // 0 - 6 00 22 44 66 88 100 105 110 115
        int i = r.nextInt(110);
        int type = 0;
        if (i <= 10) {
            type = 6;
        } else if (10 < i && i <= 34) {
            type = 5;
        } else if (34 < i && i <= 56) {
            type = 4;
        } else if (56 < i && i <= 78) {
            type = 3;
        } else if (78 < i && i <= 100) {
            type = 2;
        } else if (100 < i && i <= 105) {
            type = 1;
        } else if (105 < i && i <= 110) {
            type = 0;
        }
        // System.out.println(type);
        int[] ints = GetCards2(type);
        for (int j = 0; j < ints.length - 1; j++) {
            int b = r.nextInt(4);
            int c = ints[b];
            ints[b] = ints[j];
            ints[j] = c;
        }
        return ints;
    }

    /**
     * 机器人获取指定类型牌
     *
     * @param type
     * @return
     */
    public static int[] GetCards2(int type) {
        int[] p1 = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; // 黑桃
        int[] p2 = new int[]{13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
                25}; // 红桃
        int[] p3 = new int[]{26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
                38}; // 梅花
        int[] p4 = new int[]{39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
                51}; // 方块

        int[][] cards2 = new int[][]{p1, p2, p3, p4}; // 一副扑克牌
        int[] cards = new int[8]; // 五张牌
        cards[7] = type;
        int[] hs = random(4); // 四种花色
        int next = r.nextInt(4); // 随机选中一种花色
        int index = r.nextInt(13); // 相同花色牌抽牌位置
        int n = 0;

        // 同花顺
        if (type == 0) {
            int[] pocer = cards2[next];
            int start = r.nextInt(6) + 3;
            for (int i = 0; i < 5; i++) {
                cards[i] = pocer[start];
                start++;
            }
            int a = r.nextInt(6);
            cards[5] = cards2[next + 1 > 3 ? 0 : next + 1][a];
            cards[6] = cards2[next + 1 > 3 ? 0 : next + 1][a + 7];
            //
        }
        // 四条
        else if (type == 1) {
            // 5 3 0 -12 0-3
            int lastIndex = index > next ? index - next - 1 : next + index + 2; // 最后一个数索引
            for (int i : hs) {
                cards[n] = cards2[i][index];
                n++;
            }
            cards[4] = cards2[hs[0]][index + 1 > 12 ? 0 : index + 1];

            cards[5] = cards2[hs[2]][index + 2 > 12 ? 1 : index + 2];
            cards[6] = cards2[hs[3]][index + 3 > 12 ? 2 : index + 3];
        }
        // 葫芦
        else if (type == 2) {
            int lastIndex = index > next ? index - next - 1 : next + index + 2; // 后面对子索引
            for (int i : hs) {
                cards[n] = cards2[i][index];
                n++;
            }
            cards[3] = cards2[next > 0 ? next - 1 : next + 1][lastIndex];
            cards[4] = cards2[next][lastIndex];

            next++;
            lastIndex = index > next ? index - next - 1 : next + index + 2; // 最后一个数索引
            cards[5] = cards2[hs[2]][lastIndex];
            cards[6] = cards2[hs[3]][lastIndex];
        }
        // 同花
        else if (type == 3) {
            int[] pocer = cards2[next];
            int[] indexs = random(13);
            for (int i = 0; i < 5; i++) {
                cards[i] = pocer[indexs[i]];
            }

            int a = r.nextInt(6);
            cards[5] = cards2[next + 1 > 3 ? 0 : next + 1][a];
            cards[6] = cards2[next + 1 > 3 ? 0 : next + 1][a + 7];
        }
        // 顺子
        else if (type == 4) {
            index = r.nextInt(6) + 3;
            for (int i : hs) {
                cards[n] = cards2[i][index + n];
                n++;
            }
            cards[4] = cards2[3][index + 4];

            cards[5] = cards2[hs[2]][index - 2 < 0 ? 12 : index - 2];
            cards[6] = cards2[hs[3]][index + 2 > 12 ? 0 : index + 2];
        }
        // 三条
        else if (type == 5) {
            for (int i : hs) {
                cards[n] = cards2[i][index];
                n++;
            }
            int index3 = r.nextInt(7);
            cards[3] = cards2[next][index3 == index ? index3 + 1 : index3];
            int index4 = r.nextInt(6) + 6;

            cards[4] = cards2[next][index4 == index ? index4 - 1 : index4];
            index3++;
            index4++;
            cards[5] = cards2[next][index3 == index ? index3 + 1 : index3];
            cards[6] = cards2[next][index4 == index ? index4 - 1 : index4];
        }
        // 两对
        else if (type == 6) {
            // 第一对
            int t1 = r.nextInt(3) + 7;
            cards[0] = cards2[hs[0]][t1];
            cards[1] = cards2[hs[1]][t1];
            // 第二对
            int t2 = r.nextInt(3) + 10;
            cards[2] = cards2[hs[2]][t2];
            cards[3] = cards2[hs[3]][t2];
            // 第五张牌
            cards[4] = cards2[hs[0]][t1 - 1];

            cards[5] = cards2[hs[1]][t1 + 1 > 12 ? t1 - 2 : t1 + 1];
            cards[6] = cards2[hs[3]][t2 - 1 < 0 ? t2 + 2 : t2 - 1];
        }
        return cards;
    }

    /**
     * 打印牌
     *
     * @param cards
     */
    private static void printCards(int[] cards) {
        for (int i = 0; i < cards.length; i++) {
            String huase = "";
            if (cards[i] > 38) {
                cards[i] -= 37;
                huase = "方块";
            } else if (cards[i] > 25) {
                cards[i] -= 24;
                huase = "梅花";
            } else if (cards[i] > 12) {
                cards[i] -= 11;
                huase = "红桃";
            } else {
                cards[i] += 2;
                huase = "黑桃";
            }

            // 打印牌
            if (cards[i] == 11) {
                System.out.print(huase + "J ");
            } else if (cards[i] == 12) {
                System.out.print(huase + "Q ");
            } else if (cards[i] == 13) {
                System.out.print(huase + "K ");
            } else if (cards[i] == 14) {
                System.out.print(huase + "A ");
            } else {
                System.out.print(huase + cards[i] + " ");
            }
        }
    }
}
